package com.will.demo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Toolbar id_toolbar;
    private DrawerLayout mDLayout;
    private DrawerLayout id_dLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //toolbar控件
        id_toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        //设置toolbar
        setSupportActionBar(id_toolbar);

        //侧滑控件
        id_dLayout = (DrawerLayout) findViewById(R.id.id_dLayout);

        //侧滑布局
        NavigationView id_nav_view = (NavigationView) findViewById(R.id.id_nav_view);
        //默认选中条目
        id_nav_view.setCheckedItem(R.id.id_one);
        //条目点击监听
        id_nav_view.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        String title = (String) item.getTitle();
                        Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
                        //关闭侧滑菜单
                        id_dLayout.closeDrawers();
                        return true;
                    }
                });

        //获取侧滑菜单提示按钮
        ActionBar bar = getSupportActionBar();
        if (bar != null) {
            //显示按钮
            bar.setDisplayHomeAsUpEnabled(true);
            //按钮样式
            bar.setHomeAsUpIndicator(R.drawable.ic_navigation_drawer);
        }
    }

    //加载toolbar布局
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    //toolbar各种按钮点击响应
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.backup:
                Toast.makeText(MainActivity.this, "Backup", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(MainActivity.this, "Delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ok:
                Toast.makeText(MainActivity.this, "Watching", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home://android自带按钮
                id_dLayout.openDrawer(GravityCompat.START);
                break;
        }

        return true;
    }
}
